package eureka.consumer.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * @Author:RenPu
 * @Date: 2020/8/20 16:03
 * @Version: 1.0
 * @description: Pdf导出
 */

@RequestMapping("/Pdf")
@RestController
@Slf4j
public class PdfController {


    @GetMapping("/getPdfFile")
    public void getPdfFile(HttpServletResponse response) {

        // 设置字体样式
        BaseFont bfChinese = null;
        try {
            bfChinese = BaseFont.createFont("c://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

            Font blackFont = new Font(bfChinese);
            blackFont.setSize(12);
            blackFont.setStyle(FontFactory.HELVETICA);
            blackFont.setColor(BaseColor.BLACK);
//        红色字体
            Font redFont = new Font(bfChinese);
            redFont.setSize(12);
            redFont.setStyle(FontFactory.HELVETICA);
            redFont.setColor(BaseColor.RED);
//        橙色字体
            Font orangeFont = new Font(bfChinese);
            orangeFont.setSize(12);
            orangeFont.setStyle(FontFactory.HELVETICA);
            orangeFont.setColor(BaseColor.ORANGE);

            // 设置页面信息  A4纸，页边距
            Document doc = new Document(PageSize.A4, 90.14f, 90.14f, 72f, 72f);

            PdfWriter.getInstance(doc, response.getOutputStream());
            doc.open();
            // 授权书标题
            Paragraph title = new Paragraph("授权书（个人VIP）", blackFont);
            title.setAlignment(Element.ALIGN_CENTER);
            doc.add(title);
            doc.add(new Paragraph("\n"));

            // 设计信息表格
            // 设置 3 列表格
            PdfPTable table = new PdfPTable(3);
            // 设置表格宽度
            table.setTotalWidth(new float[]{20, 40, 40});// 这里是百分比，每列所占整个宽度的比例
            table.setTotalWidth(414.992f);// 这是设置表格的定宽
            table.setLockedWidth(true);// 这个必须设置为true,意思是按照我们设置的锁定表格的宽度
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 图片
            Image cellimg = Image.getInstance("C:\\Users\\中交安科\\Pictures\\5490c105bc65fbaaa16b0a89b8f92dc4.jpg");
            PdfPCell cell1 = new PdfPCell(cellimg, true);
            cell1.setBorderColor(BaseColor.GRAY);// 单元格边框颜色
            cell1.setPaddingLeft(10);
            cell1.setFixedHeight(100);
            cell1.setLeading(20, 1);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setVerticalAlignment(Element.ALIGN_CENTER);
            cell1.setRowspan(4);
            cell1.setFixedHeight(50);
            cell1.setTop(10);
            table.addCell(cell1);
            // 授权方
            PdfPCell cell2 = new PdfPCell(new Paragraph("授权方：xxxxx", blackFont));
            cell2.setBorderColor(BaseColor.GRAY);
            cell2.setPaddingLeft(10);
            cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell2.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell2);
            // 被授权方
            PdfPCell cell3 = new PdfPCell();
            Paragraph elements = new Paragraph();
            elements.add(new Chunk("被授权方：王大套", blackFont));
            cell3.setPhrase(elements);
            cell3.setBorderColor(BaseColor.GRAY);
            cell3.setPaddingLeft(10);
            cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell3.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell3);
            // 授权方网站
            PdfPCell cell4 = new PdfPCell(new Paragraph("授权⽅⽹站：xxxxx", blackFont));
            cell4.setBorderColor(BaseColor.GRAY);
            cell4.setPaddingLeft(10);
            cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell4.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell4);
            // 身份证/统⼀社会信⽤代码：
            PdfPCell cell5 = new PdfPCell(new Paragraph("身份证/统⼀社会信⽤代码：xxxxxx", blackFont));
            cell5.setBorderColor(BaseColor.GRAY);
            cell5.setPaddingLeft(10);
            cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell5.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell5);
            // 地址
            PdfPCell cell6 = new PdfPCell(new Paragraph("地址：xxxxxx", blackFont));
            cell6.setBorderColor(BaseColor.GRAY);
            cell6.setPaddingLeft(10);
            cell6.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell6.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell6);
            // 地址
            PdfPCell cell7 = new PdfPCell(new Paragraph("地址：xxxxxxx", blackFont));
            cell7.setBorderColor(BaseColor.GRAY);
            cell7.setPaddingLeft(10);
            cell7.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell7.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell7);
            // 设计ID
            PdfPCell cell8 = new PdfPCell();
            Paragraph elements1 = new Paragraph();
            elements1.add(new Chunk("内容id:", redFont));
            elements1.add(new Chunk("sdfasd-DALKDA-dagfsadf", blackFont));
            cell8.addElement(elements1);
            cell8.setBorderColor(BaseColor.GRAY);
            cell8.setPaddingLeft(10);
            cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell8.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell8);
            // 联系电话
            PdfPCell cell9 = new PdfPCell(new Paragraph("联系电话：xxxxxx", blackFont));
            cell9.setBorderColor(BaseColor.GRAY);
            cell9.setPaddingLeft(10);
            cell9.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell9.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell9);
            // 空白
            PdfPCell cell10 = new PdfPCell(new Paragraph(" ", blackFont));
            cell10.setBorderColor(BaseColor.GRAY);
            cell10.setPaddingLeft(10);
            cell10.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell10.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell10);
            // 空白
            PdfPCell cell11 = new PdfPCell(new Paragraph(" ", blackFont));
            cell11.setBorderColor(BaseColor.GRAY);
            cell11.setPaddingLeft(10);
            cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell11);

            // 被授权⽤户ID：
            PdfPCell cell12 = new PdfPCell(new Paragraph("被授权⽤户ID：xxxxxx", blackFont));
            cell12.setBorderColor(BaseColor.GRAY);
            cell12.setPaddingLeft(10);
            cell12.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell12.setVerticalAlignment(Element.ALIGN_TOP);
            table.addCell(cell12);
            doc.add(table);
            doc.add(new Paragraph("\n"));

            // 授权内容
            Paragraph temp = new Paragraph("1.授权内容", blackFont);
            temp.setLeading(23);
            temp.setAlignment(Element.ALIGN_LEFT);
            doc.add(temp);
            temp = new Paragraph("xxxxxx", blackFont);
            temp.setLeading(23);
            temp.setAlignment(Element.ALIGN_LEFT);
            doc.add(temp);
            doc.add(new Paragraph("\n"));// 换行

            temp = new Paragraph("2.授权范围", blackFont);
            temp.setLeading(23);
            temp.setAlignment(Element.ALIGN_LEFT);
            doc.add(temp);
            temp = new Paragraph("xxxxxx", blackFont);
            temp.setLeading(23);
            temp.setAlignment(Element.ALIGN_LEFT);
            doc.add(temp);
            doc.add(new Paragraph("\n"));// 换行

            temp = new Paragraph("xxxxx公司", blackFont);
            temp.setLeading(23);
            temp.setAlignment(Element.ALIGN_RIGHT);
            doc.add(temp);
            temp = new Paragraph("2020 年 03 月 28 日", blackFont);
            temp.setLeading(23);
            temp.setAlignment(Element.ALIGN_RIGHT);
            doc.add(temp);
            doc.add(new Paragraph("\n"));// 换行

            doc.close();

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
